package com.ymttest.business.service.productdata.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.ymatou.iapi.sellerproduct.service.AddNewSellerProductCall;
import com.ymttest.utils.FileUtil;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.compare.CompareHelper;
import com.ymttest.utils.compare.LogPathConf;
import com.ymttest.utils.compare.LoggerHelper;
import com.ymttest.utils.compare.LoggerInterface;

public class CreateProduct {
	
	
	
	
	
	public static void main(String args[]) {
		LogPathConf logPathConf = new LogPathConf(true);
		LoggerInterface logger = new LoggerHelper(logPathConf);
		CompareHelper compareHelper = new CompareHelper(logger, 5);
		
		
		//unId
		String unIdFile = System.getProperty("user.dir") + File.separator + "resource" + File.separator
						+ "CompareData" + File.separator + "unId.txt";

		List<String> unIdFileList = FileUtil.readFileAsLine(unIdFile);
		
		//brandId
		String brandIdFile = System.getProperty("user.dir") + File.separator + "resource" + File.separator
				+ "CompareData" + File.separator + "brandId.txt";

		List<String> brandIdFileList = FileUtil.readFileAsLine(brandIdFile);
		
		//brandName
		String brandNameFile = System.getProperty("user.dir") + File.separator + "resource" + File.separator
				+ "CompareData" + File.separator + "brandName.txt";

		List<String> brandNameFileList = FileUtil.readFileAsLine(brandNameFile);
		
		//productname
		String nameFile = System.getProperty("user.dir") + File.separator + "resource" + File.separator
				+ "CompareData" + File.separator + "name.txt";

		List<String> nameFileList = FileUtil.readFileAsLine(nameFile);
		
		/*//price 
		String priceFile = System.getProperty("user.dir") + File.separator + "resource" + File.separator
						+ "CompareData" + File.separator + "price.txt";

		List<String> priceFileList = FileUtil.readFileAsLine(priceFile);*/
		
		
		//unIdAndPrice
		String unIdAndPriceFile = System.getProperty("user.dir") + File.separator + "resource" + File.separator
				+ "CompareData" + File.separator + "unIdAndPrice.txt";

		List<String> unIdAndPriceFileList = FileUtil.readFileAsLine(unIdAndPriceFile);
		
		
		
		
		//int sellerId = 3383;
		int sellerId = 22121321;
		String sellerName = "cecond";
		
		
		for (int i=0;i<brandIdFileList.size();i++){
			
			CreateProdBean createProdBean = new CreateProdBean();
			
			createProdBean.setUnId(unIdFileList.get(i));
			createProdBean.setBrandId(Integer.parseInt(brandIdFileList.get(i)));
			createProdBean.setBrandName(brandNameFileList.get(i));
			createProdBean.setProductName(nameFileList.get(i));
			createProdBean.setPriceList(null);
			
			for (String unIdAndPrice : unIdAndPriceFileList){
				String[] arys = unIdAndPrice.split(",");
				String tmpUnId = arys[0];
				Double price  = Double.parseDouble(arys[1]);
				
				if (createProdBean.getUnId().equals(tmpUnId)){
					if (createProdBean.getPriceList()==null){createProdBean.setPriceList(new ArrayList<>());}
					createProdBean.getPriceList().add(price);
				}
			}
			
			try{
				if (createProdBean.getPriceList().size()>1){
					JSONObject addProductBean2 = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc15.json");
					JSONArray catalogsArray = addProductBean2.getJSONObject("Product").getJSONArray("Catalogs");
					String catalogs = catalogsArray.getJSONObject(0).toString();
					for (int k=0;k<createProdBean.getPriceList().size();k++){
						catalogsArray.put(k, new JSONObject(catalogs));
						JsonBeanHelper.updateJsonBean(catalogsArray.getJSONObject(k), MapUtil.hashMap("Price", createProdBean.getPriceList().get(k),
									"PropertyValues[0]->PropertyValues[0]->PropertyValue","red"+k));
					}
					
					Map<String, Object> updateMap = MapUtil.hashMap("UserID",sellerId,"LoginID",sellerName,
							"Product->iBrandId",createProdBean.getBrandId(),"Product->sBrandName",createProdBean.getBrandName(),"Product->sBrandEnName",createProdBean.getBrandName(),
							"Product->ProductName",createProdBean.getProductName(),"Product->Catalogs",catalogsArray);
					JsonBeanHelper.updateJsonBean(addProductBean2, updateMap);
					String productId  = new AddNewSellerProductCall().callAndGetReturnData(addProductBean2).getJSONObject("Data").getString("ProductID");
					logger.comment(productId);
					
				}else{
					JSONObject addProductBean1 = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc14.json");
					Map<String, Object> updateMap = MapUtil.hashMap("UserID",sellerId,"LoginID",sellerName,
							"Product->iBrandId",createProdBean.getBrandId(),"Product->sBrandName",createProdBean.getBrandName(),"Product->sBrandEnName",createProdBean.getBrandName(),
							"Product->ProductName",createProdBean.getProductName(),"Product->Catalogs[0]->Price",createProdBean.getPriceList().get(0));
					JsonBeanHelper.updateJsonBean(addProductBean1, updateMap);
					String productId  = new AddNewSellerProductCall().callAndGetReturnData(addProductBean1).getJSONObject("Data").getString("ProductID");
					logger.comment(productId);
				}
			}catch(Exception e ){
				logger.comment(createProdBean.getProductName()+","+createProdBean.getPriceList()+","+createProdBean.getBrandName());
				logger.comment(e.getMessage());
			}
		}
			

		
	}
}
